﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using OCBizAPI.Models;
using OCBizAPI.Repository;

namespace OCBizAPI.Controllers
{
    public class GastosController : ApiController
    {
        private OCBizAPIContext db = new OCBizAPIContext();

        // GET api/Gastos
        public IEnumerable<Gasto> GetGastoes()
        {
            return db.Gastos.AsEnumerable();
        }

        // GET api/Gastos/5
        public Gasto GetGasto(Guid id)
        {
            Gasto gasto = db.Gastos.Find(id);
            if (gasto == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return gasto;
        }

        // PUT api/Gastos/5
        public HttpResponseMessage PutGasto(Guid id, Gasto gasto)
        {
            if (ModelState.IsValid && id == gasto.Id)
            {
                db.Entry(gasto).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Gastos
        public HttpResponseMessage PostGasto(Gasto gasto)
        {
            if (ModelState.IsValid)
            {
                db.Gastos.Add(gasto);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, gasto);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = gasto.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Gastos/5
        public HttpResponseMessage DeleteGasto(Guid id)
        {
            Gasto gasto = db.Gastos.Find(id);
            if (gasto == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Gastos.Remove(gasto);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, gasto);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}